//
//  UIColor+Util.m
//  JHFocusView
//
//  Created by winter on 15/9/22.
//  Copyright (c) 2015年 winter. All rights reserved.
//

#import "UIColor+Util.h"


@implementation UIColor (Util)

+(UIColor *)randomColor
{
    CGFloat hue = (arc4random()%256/256.0);
    CGFloat saturation = (arc4random()%128/256.0) + 0.5;
    CGFloat brightness = (arc4random()%128/256.0) + 0.5;
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1.0];
}

+(UIColor *)randomColor:(CGFloat)alpha
{
    NSInteger red = (arc4random()%255) + 1;
    NSInteger green = (arc4random()%255) + 1;
    NSInteger blue = (arc4random()%255) + 1;
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha];
}

+(UIColor *)colorWithHexString:(NSString*)hexString
{
    return [UIColor colorWithHexStringWithAlpha:hexString alpha:1.0];
}


+(UIColor *)colorWithHexStringWithAlpha:(NSString*)hexString alpha:(CGFloat)alpha
{
    NSInteger length = [hexString length];
    NSAssert(length>5, @"invalid string,too short");
    NSRange range;
    range.location = length==6?0:1;
    range.length = 2;
    NSString* red = [hexString substringWithRange:range];
    range.location += 2;
    NSString* green = [hexString substringWithRange:range];
    range.location += 2;
    NSString* blue = [hexString substringWithRange:range];
    unsigned int r,g,b;
    [[NSScanner scannerWithString:red] scanHexInt:&r];
    [[NSScanner scannerWithString:green] scanHexInt:&g];
    [[NSScanner scannerWithString:blue] scanHexInt:&b];
    return [UIColor colorWithRGBA:r green:g blue:b alpha:alpha];
}


+(UIColor*) colorWithRGBA:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha
{
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha];
}
@end
